package com.isoftstone.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.isoftstone.entity.CK_Prescription;
import com.isoftstone.entity.C_Prescription;
import com.isoftstone.entity.Patient;
import com.isoftstone.entity.W_Prescription;
import com.isoftstone.service.ChargeService;

@Controller
@RequestMapping("charge")
public class ChargeController {
	private String guahao_id;
	@Autowired
	private ChargeService chargeService;
	
	@RequestMapping("ChargeList")
	public String openCharge(ModelMap modelMap){

		return "ChargeList";
	}
	
	@RequestMapping("getPatient")
	@ResponseBody
	public List<Patient> getPatient(String patient_name,String patient_idcard){
		List<Patient> patient = chargeService.getPatient(patient_name,patient_idcard);
		guahao_id = patient.get(0).getGuahao_id();
		return patient;
	}
	
	@RequestMapping("getCkPrescription")
	@ResponseBody
	public List<CK_Prescription> getCkPrescription() {
		List<CK_Prescription> cklist = chargeService.getCkPrescription(guahao_id);
		return cklist;
	}
	
	@RequestMapping("getWmPrescription")
	@ResponseBody
	public List<W_Prescription> getWmPrescription() {
		List<W_Prescription> wmlist = chargeService.getWmPrescription(guahao_id);
		return wmlist;
	}
	
	@RequestMapping("getCmPrescription")
	@ResponseBody
	public List<C_Prescription> getCmPrescription() {
		List<C_Prescription> cmlist = chargeService.getCmPrescription(guahao_id);
		return cmlist;
	}
	
	@RequestMapping("wmSum")
	@ResponseBody
	public double wmSum() {
		List<W_Prescription> wmlist = chargeService.getWmPrescription(guahao_id);
		double wmSum = 0;
		for(W_Prescription i:wmlist){
			wmSum += i.getWm_amount();
		}
		return wmSum;
	}
	
	@RequestMapping("cmSum")
	@ResponseBody
	public double cmSum() {
		List<C_Prescription> cmlist = chargeService.getCmPrescription(guahao_id);
		double cmSum = 0;
		for(C_Prescription i:cmlist){
			cmSum += i.getCm_amount();
		}
		return cmSum;
	}
	
	@RequestMapping("ckSum")
	@ResponseBody
	public double ckSum() {
		List<CK_Prescription> cklist = chargeService.getCkPrescription(guahao_id);
		double ckSum = 0;
		for(CK_Prescription i:cklist){
			ckSum += i.getCk_amount();
		}
		return ckSum;
	}
	
	@RequestMapping("allSum")
	@ResponseBody
	public double allSum() {
		List<CK_Prescription> cklist = chargeService.getCkPrescription(guahao_id);
		double ckSum = 0;
		for(CK_Prescription i:cklist){
			ckSum += i.getCk_amount();
		}
		List<C_Prescription> cmlist = chargeService.getCmPrescription(guahao_id);
		double cmSum = 0;
		for(C_Prescription i:cmlist){
			cmSum += i.getCm_amount();
		}
		List<W_Prescription> wmlist = chargeService.getWmPrescription(guahao_id);
		double wmSum = 0;
		for(W_Prescription i:wmlist){
			wmSum += i.getWm_amount();
		}
		double allSum = cmSum+wmSum+ckSum;
		return allSum;
	}
	
	@RequestMapping("insertFee")
	@ResponseBody
	public void insertFees(Date ls_data){
		List<W_Prescription> wmlist = chargeService.getWmPrescription(guahao_id);
		for(W_Prescription i:wmlist){
			String wm_id=i.getWm_id();
			Double wm_amount_payed=i.getWm_amount();
			chargeService.wmUpdate(guahao_id,wm_amount_payed,wm_id);
			chargeService.insertFee(wm_id,wm_amount_payed,ls_data);
		}
		List<C_Prescription> cmlist = chargeService.getCmPrescription(guahao_id);
		for(C_Prescription i:cmlist){
			String cm_id=i.getCm_id();
			Double cm_amount_payed=i.getCm_amount();
			chargeService.cmUpdate(guahao_id,cm_amount_payed,cm_id);
			chargeService.insertFee(cm_id,cm_amount_payed,ls_data);
		}
		List<CK_Prescription> cklist = chargeService.getCkPrescription(guahao_id);
		for(CK_Prescription i:cklist){
			String ck_id=i.getCk_id();
			Double ck_amount_payed=i.getCk_amount();			
			chargeService.ckUpdate(guahao_id,ck_amount_payed,ck_id);
			chargeService.insertFee(ck_id,ck_amount_payed,ls_data);
		}		
		return;
	}
}
